# Maintainer: Philipp Smirnov https://github.com/sad-poet

_realname=ada-libfswatch
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=25.1.r21.838480d
pkgrel=3
_branch=25.1
_commit=838480d8fca344d9f8a78341113ceb4ed5cf2222
pkgdesc="Ada binding to the libfswatch library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://github.com/AdaCore/ada_libfswatch"
msys2_references=(
  'aur: ada-libfswatch'
  'gentoo: '
)
license=('spdx:GPL-3.0-or-later WITH GCC-exception-3.1')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-ada"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-core"
         "${MINGW_PACKAGE_PREFIX}-fswatch")
makedepends=("${MINGW_PACKAGE_PREFIX}-gprbuild"
             "git")
options=(!emptydirs)
source=("${_realname}"::"git+https://github.com/AdaCore/ada_libfswatch.git#commit=${_commit}"
        '0001-Use-system-libfswatch-install.patch'
        '0002-Build-dynamic-library.patch'
        '0003-Makefile-allow-passing-custom-flags-to-gprbuild.patch')
sha256sums=('b85cbe2cbd8740f0ab74b96c5915bfd51a815afcafd5b2d6d624eba59c880673'
            '534b4737f69f3eab397e512a334583d936c7f22d812ec87580d78bd7694d74ef'
            '57fca6d3ede522986bfad63d15e7be84bce29d3223b6898dd96623c6fed1433b'
            '9d1f8af2fc1ff42381badb54ee499324517172c14bcb6d234e29b0a53832bb3a')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "%s.r%s.%s" "$_branch" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/${_realname}"
  apply_patch_with_msg \
    0001-Use-system-libfswatch-install.patch \
    0002-Build-dynamic-library.patch \
    0003-Makefile-allow-passing-custom-flags-to-gprbuild.patch
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -r "${srcdir}/${_realname}" "${srcdir}"/build-${MSYSTEM}
  cd "${srcdir}"/build-${MSYSTEM}

  make
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  make install DESTDIR="${pkgdir}${MINGW_PREFIX}"

  # ada_libfswatch wants to replace libfswatch.dll,
  # which is already provided by the fswatch package
  rm "${pkgdir}/${MINGW_PREFIX}/bin/libfswatch.dll"

  install -Dm644 -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname} \
    "${srcdir}"/${_realname}/COPYING*
}
